package com.yichen.rent.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.yichen.rent.dao.CostDao;
import com.yichen.rent.pojo.Cost;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class CostService {

	@Autowired
	private CostDao costDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Cost> findAll() {
		return costDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Cost> findSearch(Map whereMap, int page, int size) {
		Specification<Cost> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return costDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Cost> findSearch(Map whereMap) {
		Specification<Cost> specification = createSpecification(whereMap);
		return costDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public Cost findById(String id) {
		return costDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param cost
	 */
	public void add(Cost cost) {
		cost.setId( idWorker.nextId()+"" );
		costDao.save(cost);
	}

	/**
	 * 修改
	 * @param cost
	 */
	public void update(Cost cost) {
		costDao.save(cost);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		costDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<Cost> createSpecification(Map searchMap) {

		return new Specification<Cost>() {

			@Override
			public Predicate toPredicate(Root<Cost> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 编号
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.equal(root.get("id").as(String.class), searchMap.get("id")));
                }
                // 房源编号
                if (searchMap.get("house_id")!=null && !"".equals(searchMap.get("house_id"))) {
                	predicateList.add(cb.equal(root.get("house_id").as(String.class), searchMap.get("house_id")));
                }
                // 付款方式
                if (searchMap.get("payment")!=null && !"".equals(searchMap.get("payment"))) {
                	predicateList.add(cb.like(root.get("payment").as(String.class), "%"+(String)searchMap.get("payment")+"%"));
                }
                // 租金
                if (searchMap.get("rent")!=null && !"".equals(searchMap.get("rent"))) {
                	predicateList.add(cb.like(root.get("rent").as(String.class), "%"+(String)searchMap.get("rent")+"%"));
                }
                // 押金
                if (searchMap.get("cash")!=null && !"".equals(searchMap.get("cash"))) {
                	predicateList.add(cb.like(root.get("cash").as(String.class), "%"+(String)searchMap.get("cash")+"%"));
                }
                // 服务费
                if (searchMap.get("service_charge")!=null && !"".equals(searchMap.get("service_charge"))) {
                	predicateList.add(cb.like(root.get("service_charge").as(String.class), "%"+(String)searchMap.get("service_charge")+"%"));
                }
                // 中介费
                if (searchMap.get("agency_fee")!=null && !"".equals(searchMap.get("agency_fee"))) {
                	predicateList.add(cb.like(root.get("agency_fee").as(String.class), "%"+(String)searchMap.get("agency_fee")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
